package com.bingo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bingo.common.R;
import com.bingo.entity.BaseData;
import com.bingo.service.BaseDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Bingo
 * @version 1.0
 * @description 系数控制类
 * @date 2024/5/6 21:31
 */

@RestController
@RequestMapping("/baseData")
public class BaseDataController {

    private BaseDataService baseDataService;
    @Autowired
    public void setBaseDataService(BaseDataService baseDataService) {
        this.baseDataService = baseDataService;
    }

    @PostMapping("/update")
    public R updateBaseData(@RequestBody BaseData baseData) {
        QueryWrapper<BaseData> wrapper = new QueryWrapper<>();
        wrapper.eq("name", baseData.getName());
        BaseData one = baseDataService.getOne(wrapper);
        if (one == null) {
            return R.error("不存在该系数");
        }
        one.setValue(baseData.getValue());
        boolean b = baseDataService.updateById(one);
        return b ? R.success("修改成功") : R.error("修改失败");
    }

    @PostMapping("/add")
    public R addBaseData(@RequestBody BaseData baseData) {
        QueryWrapper<BaseData> wrapper = new QueryWrapper<>();
        wrapper.eq("name", baseData.getName());
        BaseData one = baseDataService.getOne(wrapper);
        if (one != null) {
            return R.error("已存在该系数");
        }
        boolean save = baseDataService.save(baseData);
        return save ? R.success("添加成功") : R.error("添加失败");
    }

    @PostMapping("/delete")
    public R deleteBaseData(@RequestBody BaseData baseData) {
        QueryWrapper<BaseData> wrapper = new QueryWrapper<>();
        wrapper.eq("name", baseData.getName());
        BaseData one = baseDataService.getOne(wrapper);
        if (one == null) {
            return R.error("不存在该系数");
        }
        boolean b = baseDataService.removeById(one.getId());
        return b ? R.success("删除成功") : R.error("删除失败");
    }

    @PostMapping("/select")
    public R selectBaseData() {
        List<BaseData> list = baseDataService.list();
        return R.success(list);
    }



}
